package cn.dgut.training.dto.user;


import cn.dgut.training.dto.BaseDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class AddressDTO extends BaseDTO {

    private Long id;
    private String name;
    private Integer userId;
    private String province;
    private String city;
    private String county;
    private String addressDetail;
    private String tel;
    private Boolean isDefault;



    /*
    *id、name、userId查询条件
     */
    public static QueryWrapper getWrapper(AddressDTO addressDTO){

        QueryWrapper queryWrapper=new QueryWrapper();

        if(addressDTO.getUserId()!=null){
            queryWrapper.like("user_id",addressDTO.getUserId());
        }
        if(addressDTO.getId()!=null){
            queryWrapper.like("id",addressDTO.getId());
        }
        if(!StringUtils.isBlank(addressDTO.getName())){
            queryWrapper.like("name",addressDTO.getName());
        }
        if(!StringUtils.isBlank(addressDTO.getOrderBy())){
            if("desc".equals(addressDTO.getSort())){
                queryWrapper.orderByDesc(addressDTO.getOrderBy());
            }else {
                queryWrapper.orderByAsc(addressDTO.getOrderBy());
            }
        }
        return queryWrapper;
    }

    /*
    *默认地址查询条件
     */
    public static QueryWrapper getDefault(AddressDTO addressDTO){

        QueryWrapper wrapper=new QueryWrapper();

        if(addressDTO.getUserId()!=null){
            wrapper.like("user_id",addressDTO.getUserId());
            wrapper.like("is_default",1);
        }
        if(!StringUtils.isBlank(addressDTO.getOrderBy())){
            if("desc".equals(addressDTO.getSort())){
                wrapper.orderByDesc(addressDTO.getOrderBy());
            }else {
                wrapper.orderByAsc(addressDTO.getOrderBy());
            }
        }
        return wrapper;
    }

    /*
    *获取行政区域表条件
     */
    public static QueryWrapper getRegionName(Long id) {

        QueryWrapper queryWrapper = new QueryWrapper();
        if (id!=null){
            queryWrapper.like("id",id);
        }
        return queryWrapper;
    }

}
